package com.yaowk.system.server.controller;

import com.yaowk.common.api.BaseException;
import com.yaowk.common.api.PageReq;
import com.yaowk.common.api.PageResp;
import com.yaowk.common.api.model.result.CheckboxTree;
import com.yaowk.common.api.model.result.Tree;
import com.yaowk.security.utils.ISecurityUtils;
import com.yaowk.system.api.RoleApi;
import com.yaowk.system.api.model.Menu;
import com.yaowk.system.api.model.Role;
import com.yaowk.system.server.service.IMenuService;
import com.yaowk.system.server.service.IRoleService;
import com.yaowk.system.server.utils.MenuUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

@RestController
@Api(tags = "角色")
public class RoleController implements RoleApi {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IMenuService menuService;
    @Autowired
    private ISecurityUtils securityUtils;

    private final String ROLE_ID_ERROR = "只能操作自己创建的角色";

    @Override
    @ApiOperation("角色详情")
    public Role findOne(@PathVariable Long id) {
        Role role = roleService.findById(id);
        if (!role.getCreateUserId().equals(securityUtils.getUserId())) {
            throw new BaseException(ROLE_ID_ERROR);
        }
        return role;
    }

    @Override
    @ApiOperation("角色列表")
    public PageResp<Role> list(Role role, PageReq pageReq) {
        role.setCreateUserId(securityUtils.getUserId());
        PageReq page = roleService.findAll(role, pageReq);
        return (PageResp<Role>) page;
    }

    @Override
    @ApiOperation("保存角色")
    public Role save(Role role) {
        role.setCreateUserId(securityUtils.getUserId());
        role = roleService.save(role);
        return role;
    }

    @Override
    @ApiOperation("删除角色")
    public void delete(@PathVariable Long id) {
        Role role = roleService.findById(id);
        if (role.getCreateUserId().equals(securityUtils.getUserId())) {
            roleService.deleteById(id);
            return;
        } else {
            throw new BaseException(ROLE_ID_ERROR);
        }
    }

    @Override
    @ApiOperation("菜单列表")
    public CheckboxTree<Tree<Menu, Long>, Long> menuList(@PathVariable Long roleId) {
        Role role = roleService.findById(roleId);
        if (!role.getCreateUserId().equals(securityUtils.getUserId())) {
            throw new BaseException(ROLE_ID_ERROR);
        } else {
            List<Menu> menus = menuService.findAllByRoleId(roleId);
            List<Menu> userMenus = menuService.findAllByUserId(securityUtils.getUserId());
            CheckboxTree<Tree<Menu, Long>, Long> tree = new CheckboxTree<>();

            // 整理用户的菜单树
            Menu menuRoot = menuService.findById(1l);
            Tree<Menu, Long> root = new Tree<>();
            root.setValue(menuRoot);
            root.setKey(menuRoot.getId());
            root.setLabel(menuRoot.getName());
            MenuUtil.findMenuChildren(userMenus, root);
            tree.getData().add(root);

            Set<Long> checkedIds = tree.getCheckedKeys();
            for (Menu menu : menus) {
                checkedIds.add(menu.getId());
            }
            return tree;
        }
    }

    @Override
    @ApiOperation("修改菜单关系")
    public void menuEdit(@PathVariable Long roleId, @RequestParam(required = false) List<Long> menuId) {
        Role role = roleService.findById(roleId);
        if (!role.getCreateUserId().equals(securityUtils.getUserId())) {
            throw new BaseException(ROLE_ID_ERROR);
        } else {
            roleService.menuEdit(roleId, menuId);
        }
    }


}
